<script setup>
import { ref } from 'vue';

defineOptions({ name: 'ClassifySearch' });

const activeName = ref(['classify-search']);

interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

// 使用分类搜索参数类型
const model = defineModel('model', { required: true });

function reset() {
  if (model.value) {
    // 重置分类搜索参数
    if ('status' in model.value) model.value.status = -1;      // 状态
    if ('categoryName' in model.value) model.value.categoryName = ''; // 分类名称
  }
  emit('reset');
}

function search() {
  // 直接触发搜索事件
  emit('search');
}
</script>

<template>
  <ElCard class="card-wrapper">
    <ElCollapse v-model="activeName">
      <ElForm :model="model">
        <ElRow :gutter="24" align="middle" justify="center" class="items-center">
          <!-- 状态选择 -->
          <ElCol :lg="5" :md="8" :sm="12">
            <ElFormItem label="状态:" prop="status">
              <ElSelect v-model="model.status" placeholder="请选择" clearable>
                <ElOption label="全部" :value="-1"></ElOption>
                <ElOption label="正常" :value="1"></ElOption>
                <ElOption label="停用" :value="0"></ElOption>
              </ElSelect>
            </ElFormItem>
          </ElCol>

          <!-- 分类名称输入 -->
          <ElCol :lg="5" :md="8" :sm="12">
            <ElFormItem label="分类名称:" prop="categoryName">
              <ElInput v-model="model.categoryName" placeholder="请输入分类名称" clearable />
            </ElFormItem>
          </ElCol>

          <!-- 操作按钮 -->
          <ElCol :lg="6" :md="24" :sm="24">
            <ElFormItem>
              <ElSpace class="w-full justify-end flex items-center" alignment="center">
                <ElButton @click="reset">
                  <template #icon>
                    <icon-ic-round-refresh class="text-icon" />
                  </template>
                  重置
                </ElButton>

                <ElButton type="primary" plain @click="search">
                  <template #icon>
                    <icon-ic-round-search class="text-icon" />
                  </template>
                  搜索
                </ElButton>
              </ElSpace>
            </ElFormItem>
          </ElCol>
        </ElRow>
      </ElForm>
    </ElCollapse>
  </ElCard>
</template>

<style scoped>
.items-center {
  display: flex;
  align-items: center;
}

:deep(.el-form-item) {
  margin-bottom: 0;
}

:deep(.el-form-item__content) {
  display: flex;
  align-items: center;
}
</style>